package org.dromara.system.controller;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.system.domain.bo.WbMinuteFileBo;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.WbCandidateFileVo;
import org.dromara.system.domain.bo.WbCandidateFileBo;
import org.dromara.system.service.IWbCandidateFileService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 中标候选人附件
 *
 * @author Lion Li
 * @date 2024-05-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/candidateFile")
public class WbCandidateFileController extends BaseController {

    private final IWbCandidateFileService wbCandidateFileService;

    private final ISysOssService ossService;
    /**
     * 查询中标候选人附件列表
     */
    @SaCheckPermission("system:candidateFile:list")
    @GetMapping("/list")
    public TableDataInfo<WbCandidateFileVo> list(WbCandidateFileBo bo, PageQuery pageQuery) {
        return wbCandidateFileService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出中标候选人附件列表
     */
    @SaCheckPermission("system:candidateFile:export")
    @Log(title = "中标候选人附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WbCandidateFileBo bo, HttpServletResponse response) {
        List<WbCandidateFileVo> list = wbCandidateFileService.queryList(bo);
        ExcelUtil.exportExcel(list, "中标候选人附件", WbCandidateFileVo.class, response);
    }

    /**
     * 获取中标候选人附件详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:candidateFile:query")
    @GetMapping("/{id}")
    public R<WbCandidateFileVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wbCandidateFileService.queryById(id));
    }

    /**
     * 新增中标候选人附件
     */
    @SaCheckPermission("system:candidateFile:add")
    @Log(title = "中标候选人附件", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WbCandidateFileBo bo) {
        return toAjax(wbCandidateFileService.insertByBo(bo));
    }

    /**
     * 修改中标候选人附件
     */
    @SaCheckPermission("system:candidateFile:edit")
    @Log(title = "中标候选人附件", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WbCandidateFileBo bo) {
        return toAjax(wbCandidateFileService.updateByBo(bo));
    }

    /**
     * 删除中标候选人附件
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:candidateFile:remove")
    @Log(title = "中标候选人附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wbCandidateFileService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 上传OSS对象存储
     *
     * @param file 文件
     */
    @Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> upload(@RequestPart("file") MultipartFile file, String candidateId) {
        if (ObjectUtil.isNull(file) && StringUtils.isEmpty(candidateId)) {
            return R.fail("上传文件不能为空且项目ID不能为空");
        }
        SysOssVo oss = ossService.upload(file);
        WbCandidateFileBo wbCandidateFileBo = new WbCandidateFileBo();
        wbCandidateFileBo.setFileName(oss.getFileName());
        wbCandidateFileBo.setCandidateId(Long.valueOf(candidateId));
        wbCandidateFileBo.setUrl(oss.getUrl());
        wbCandidateFileBo.setOssId(oss.getOssId());
        wbCandidateFileBo.setFileOriginalName(oss.getOriginalName());
        wbCandidateFileService.insertByBo(wbCandidateFileBo);
        return R.ok();
    }
}
